package com.ms.p2p.utils.impl;

import com.ms.common.utils.AppContext;
import com.ms.common.utils.MsDateUtils;
import com.ms.p2p.entity.loan.Loan;
import com.ms.p2p.entity.loan.LoanDueIn;
import com.ms.p2p.entity.loan.LoanTenderRecord;
import com.ms.p2p.enums.LoanRepayStyle;
import com.ms.p2p.utils.AverageCapitalPlusInterestUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class LoanDueInInfo implements com.ms.p2p.utils.LoanDueInInfo {



    public LoanDueIn equalPrincipalAndInterest(Loan borrow, LoanTenderRecord borrowTender, int i,String ip) {

        LoanDueIn borrowDueIn = new LoanDueIn();
        borrowDueIn.setDuein_status(0);
        borrowDueIn.setDuein_periods(i + 1);

        borrowDueIn.setDebt_user_id(borrowTender.getUser_id());
        borrowDueIn.setTender_id(borrowTender.getId());

        borrowDueIn.setDuein_time(MsDateUtils.getMonthTime(i + 1));
            // 等额本息每月还款总额
        borrowDueIn.setDuein_account(AverageCapitalPlusInterestUtils.getPerMonthPrincipalInterest(borrow.getAccount().doubleValue(),borrow.getApr().doubleValue()/100,borrow.getTime_limit()));
        borrowDueIn.setDuein_capital(AverageCapitalPlusInterestUtils.getPerMonthPrincipal(borrow.getAccount().doubleValue(),borrow.getApr().doubleValue()/100,borrow.getTime_limit()).get(i+1));
        borrowDueIn.setDuein_interest(borrowDueIn.getDuein_account().subtract(borrowDueIn.getDuein_capital()));
        borrowDueIn.setAddtime(MsDateUtils.getTime());
        borrowDueIn.setAddip(ip);
        return borrowDueIn;
    }


    public LoanDueIn lumpSumDebt(Loan borrow, LoanTenderRecord borrowTender,String ip ) {
        BigDecimal repay_account = new BigDecimal(0);
        LoanDueIn borrowDueIn = new LoanDueIn();
        borrowDueIn.setDuein_status(0);
        borrowDueIn.setDuein_periods(1);
        // 设置债权人
        borrowDueIn.setDebt_user_id(borrowTender.getUser_id());
        borrowDueIn.setTender_id(borrowTender.getId());

        if (borrow.getIs_day() == 1) {
            borrowDueIn.setDuein_time(MsDateUtils.getDayTime(borrow.getTime_limit()));
            repay_account = borrowTender.getSuccess_money()
                    .multiply(borrow.getApr().divide(new BigDecimal(36000), 10, 4))
                    .multiply(new BigDecimal(borrow.getTime_limit())).setScale(2, 4);
        } else {
            borrowDueIn.setDuein_time(MsDateUtils.getMonthTime(borrow.getTime_limit()));
            repay_account = borrowTender.getSuccess_money()
                    .multiply(borrow.getApr().divide(new BigDecimal(1200), 10, 4))
                    .multiply(new BigDecimal(borrow.getTime_limit())).setScale(2, 4);
        }
        // 一次性还本付息
        borrowDueIn.setDuein_capital(borrowTender.getSuccess_money()); // 本金
        borrowDueIn.setDuein_account(borrowDueIn.getDuein_capital().add(repay_account));
        borrowDueIn.setDuein_interest(repay_account);// 利息，投标金额*利息*月份
        borrowDueIn.setAddtime(MsDateUtils.getTime());
        borrowDueIn.setAddip(ip);
        return  borrowDueIn;

    }


    public LoanDueIn equalInstallmentPayment(Loan loan, LoanTenderRecord borrowTender, int order, String dueinTime,String ip) {
        LoanDueIn borrowDueIn = new LoanDueIn();
        borrowDueIn.setDuein_status(0);
        borrowDueIn.setDuein_periods(order + 1);
        // 设置债权人
        borrowDueIn.setDebt_user_id(borrowTender.getUser_id());
        borrowDueIn.setTender_id(borrowTender.getId());


        borrowDueIn.setDuein_time(dueinTime);
        if (loan.getPay_style().equals(LoanRepayStyle.STYLE_4.getValue().toString())
                || loan.getPay_style().equals(LoanRepayStyle.STYLE_5.getValue().toString())
                || loan.getPay_style().equals(LoanRepayStyle.STYLE_6.getValue().toString())) {
            borrowDueIn.setDuein_capital(borrowTender.getSuccess_money().divide(new BigDecimal(loan.getTime_limit()), 10, 4).setScale(2, BigDecimal.ROUND_DOWN));
            BigDecimal interest = borrowTender.getSuccess_money().multiply(loan.getApr().divide(new BigDecimal(1200), 10, 4))
                    .multiply(BigDecimal.valueOf(loan.getTime_limit())).setScale(2, 4).divide(new BigDecimal(loan.getTime_limit()), 10, 4).setScale(2, BigDecimal.ROUND_DOWN);
            borrowDueIn.setDuein_interest(interest);
            borrowDueIn.setDuein_account(borrowDueIn.getDuein_capital().add(interest).setScale(2, BigDecimal.ROUND_DOWN));

        }
        borrowDueIn.setAddtime(MsDateUtils.getTime());
        borrowDueIn.setAddip(ip);
        return  borrowDueIn;
    }


    public LoanDueIn FirstCouponLaterEdition(Loan borrow, LoanTenderRecord borrowTender, int i,String ip) {
        LoanDueIn borrowDueIn = new LoanDueIn();
        borrowDueIn.setDuein_status(0);
        borrowDueIn.setDuein_periods(i + 1);
        // 设置债权人
        borrowDueIn.setDebt_user_id(borrowTender.getUser_id());
        borrowDueIn.setTender_id(borrowTender.getId());

        borrowDueIn.setDuein_time(MsDateUtils.getMonthTime(i + 1));

        if (borrow.getPay_style().equals("3")) {
            // 按月付息
            BigDecimal repay_account = borrowTender.getSuccess_money()
                    .multiply(borrow.getApr().divide(new BigDecimal(1200), 10, 4)).setScale(2, 4);
            borrowDueIn.setDuein_account(repay_account);
            borrowDueIn.setDuein_interest(repay_account);
            borrowDueIn.setDuein_capital(new BigDecimal(0));
            if (i == borrow.getTime_limit() - 1) {
                borrowDueIn.setDuein_account(repay_account.add(borrowTender.getSuccess_money()));
                borrowDueIn.setDuein_capital(borrowTender.getSuccess_money());
            }
        }
        borrowDueIn.setAddtime(MsDateUtils.getTime());
        borrowDueIn.setAddip(ip);
        return  borrowDueIn;
    }
}
